package com.simtop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.simtop.aop.SystemServiceLog;
import com.simtop.domain.news.PolicyInformation;
import com.simtop.domain.news.PolicyInformationVo;
import com.simtop.exception.CustomException;
import com.simtop.mapper.PolicyInformationMapper;
import com.simtop.service.PolicyInformationService;
import com.simtop.utils.FileUploadAndDownload;
import com.simtop.utils.ifnull.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lvyongwei
 */
@Service
public class PolicyInformationServiceImpl implements PolicyInformationService {

    @Autowired
    private PolicyInformationMapper policyInformationMapper;

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    /**
     * 获取新闻资讯信息
     * @param policyInformation
     * @return
     */
    @Override
    public List<PolicyInformationVo> getPolicyInformationList(PolicyInformation policyInformation) {
        return policyInformationMapper.getPolicyInformationList(policyInformation);
    }

    /**
     * 添加新闻资讯内容
     * @param policyInformation 新闻资讯实体
     * @return
     */
    @Override
    @SystemServiceLog(description = "添加新闻资讯内容")
    public boolean insertPolicyInformation(PolicyInformation policyInformation, MultipartFile photo, List<MultipartFile> files) {
        if(policyInformation.getCreateTime() == null){
            policyInformation.setCreateTime(new Date());
        }

        if(photo!=null || photo.getOriginalFilename().length()!=0){
                String photoPath = fileUploadAndDownload.upload2(photo, "policyInformation");
                policyInformation.setPolicyPhoto(photoPath);
                if(StringUtils.isEmpty(photoPath)){
                    throw new CustomException("封面内容上传失败");
                }
        }
        if(files.size() > 0){
            List<JSONObject> lists = new ArrayList<JSONObject>();
            for(MultipartFile multipartFile:files){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name",multipartFile.getOriginalFilename());
                String photoPath = fileUploadAndDownload.upload2(multipartFile, "policyInformation");
                jsonObject.put("path",photoPath);
                lists.add(jsonObject);
            }
            if(lists.size() == 0){
                throw new CustomException("附件内容上传失败");
            }
            policyInformation.setPolicyAccessory(JSONObject.toJSONString(lists));
        }
        return policyInformationMapper.insertPolicyInformation(policyInformation);
    }

    /**
     * 修改新闻资讯
     * @param policyInformation
     * @return
     */
    @Override
    @SystemServiceLog(description = "修改新闻资讯内容")
    public boolean updatePolicyInformation(PolicyInformation policyInformation, MultipartFile photo, List<MultipartFile> files) {

        if(photo!=null && photo.getOriginalFilename().length()!=0){
            String photoPath = fileUploadAndDownload.upload2(photo, "policyInformation");
            policyInformation.setPolicyPhoto(photoPath);
            if(StringUtils.isEmpty(photoPath)){
                throw new CustomException("封面内容上传失败");
            }
        }
        if(files.size() > 0){
            List<JSONObject> lists = new ArrayList<JSONObject>();
            for(MultipartFile multipartFile:files){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name",multipartFile.getOriginalFilename());
                String photoPath = fileUploadAndDownload.upload2(multipartFile, "policyInformation");
                jsonObject.put("path",photoPath);
                lists.add(jsonObject);
            }
            if(lists.size() == 0){
                throw new CustomException("附件内容上传失败");
            }
            policyInformation.setPolicyAccessory(JSONObject.toJSONString(lists));
        }
        return policyInformationMapper.updatePolicyInformation(policyInformation);
    }

    /**
     * 删除新闻资讯
     * @param ids
     * @return
     */
    @Override
    @SystemServiceLog(description = "删除新闻资讯内容")
    public boolean deletePolicyInformation(List<Long> ids) {
        return policyInformationMapper.deletePolicyInformation(ids);
    }

    /**
     * 根据id获取资讯详情
     * @param policyInformationId
     * @return
     */
    @Override
    public PolicyInformation getPolicyInformationById(Long policyInformationId){
        return policyInformationMapper.getPolicyInformationById(policyInformationId);
    }
}
